import { Attributes, FindOptions, Model, ModelStatic } from "sequelize";

export class Base<TModelAttributes, TCreationAttributes> extends Model<TModelAttributes, TCreationAttributes> {
  static findPagingList () {
    console.log("findPagingList from Class:", this.name);
  }

  static async findById<T extends Model> (this: ModelStatic<T>, id: number, attributesOrOptions?: string[] | FindOptions<Attributes<T>>) {
    const options: FindOptions = {
      where: { id }
    };

    if (attributesOrOptions) {
      if (Array.isArray(attributesOrOptions)) {
        options.attributes = attributesOrOptions;
      } else {
        Object.assign(options, attributesOrOptions);
      }
    }

    return await this.findOne!(options);
  }
}
